const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        UID: 0,
        Key: '',
        IsSSK: 0,
        ReginLev: 0,
        Regin: 0,
        RTType: 0,
        RTID: 0,
        Mode: 0,
        ModeArr:[{name:'可提供上门回收',code:1},{name:'只接受送货上门',code:2}],
        ModeArrIdx:-1,
        shaixuan:false,
        FenShu: 0,
        FenShuArrIdx:-1,
        FenShuArr:[{name:'5分',code:1},{name:'4分',code:2},{name:'3分以上',code:3},{name:'3分以下',code:4},],
        intCurrPage: 1,
        list: [],
        intPageCount: 0,
        more: false,
        CityNameTit: '全国',
        ClassName: '全部分类',
        ProvCity: [],
        ProvCityShow: false,
        ProvCityIcon: 'icon-xiangxia',
        selectS: false,
        selectClass: false,
        animationData: {},
        Province: 0,
        ProvCity: [],
        ProvName: '全国',
        CityArr: [],
        City: 0,
        CityName: '',
        AreaArr: [],
        Area: 0,
        AreaName: '',
        RecType:[],
        ProductOne:0,
        ProductOneName:'全部',
        ProductTypeArr:[],
        ProductType:0,
        ProductTypeName:'',
        ProductThreeArr:[],
        ProductThree:0,
        ProductThreeName:'',
        class_step:1,
        tit_step: 1,
        listGundong: false,
        lsCityName: '',
        mores:false
    },
    clearinput:function () {
       this.setData({
            Key:''
       })
    },
    shaixuanBtn:function () {
        this.shaixuanH();
        this.setData({
            list: [],
        })
        this.getFJ_Get_RecListKey();
    },
    selMode:function (e) {
        var mode = this.data.Mode;
        if (mode == e.currentTarget.dataset.code) {
            this.setData({
                Mode:0,
                ModeArrIdx:-1
            })
        }else{
            this.setData({
                Mode:e.currentTarget.dataset.code,
                ModeArrIdx:e.currentTarget.dataset.code-1
            })
        }
    },
    selFenshu:function (e) {
        var FenShu = this.data.FenShu;
        if (FenShu == e.currentTarget.dataset.code) {
            this.setData({
                FenShu:0,
                FenShuArrIdx:-1
            })
        }else{
            this.setData({
                FenShu:e.currentTarget.dataset.code,
                FenShuArrIdx:e.currentTarget.dataset.code-1
            })
        }        
    },
    addClass:function () {
        var class_step = this.data.class_step;
        var ProductOne = this.data.ProductOne;
        var ProductOneName = this.data.ProductOneName;
        var ProductType = this.data.ProductType;
        var ProductTypeName = this.data.ProductTypeName;
        var ProductThree = this.data.ProductThree;
        var ProductThreeName = this.data.ProductThreeName;
        if (class_step == 3 && ProductThree != 0) {
            this.getSelectClass(3,ProductThree,ProductThreeName);
        }else if (class_step == 3 && ProductThree == 0) {
            this.getSelectClass(2,ProductType,ProductTypeName);
        }
        if (class_step == 2 && ProductType != 0) {
            this.setData({
                ProductThreeArr: [],
                ProductThree: 0,
                ProductThreeName: '', 
            })
            this.getSelectClass(2,ProductType,ProductTypeName);
        }else if (class_step == 2 && ProductType == 0) {
            this.setData({
                ProductThreeArr: [],
                ProductThree: 0,
                ProductThreeName: '', 
            })            
            this.getSelectClass(1,ProductOne,ProductOneName);
        }
    },
    selectC3: function(e) {
        this.setData({
            ProductThree: e.currentTarget.dataset.id,
            ProductThreeName: e.currentTarget.dataset.name,                
        })        
        if (e.currentTarget.dataset.id == 0) {
            this.getSelectClass(2,this.data.ProductType,this.data.ProductTypeName);
        }
    },    
    selectC2: function(e) {
        var api_input = {
            lev: 3,
            pid: e.currentTarget.dataset.id
        }
        var that = this;
        if (e.currentTarget.dataset.id != 0) {
            // app.getDaoPostN('api/FJDispose/FJ_Get_RecTypeByID_New', api_input, function(r) {
            //     if (r.code == 200) {
            //         var ss = {id: 0,code: "0",RT_classname: "全部"}
            //         r.data.unshift(ss); 
            //         that.setData({
            //             ProductThreeArr: r.data,
            //             class_step: 3,
            //             ProductThreeName: '全部',
            //             ProductTypeName: e.currentTarget.dataset.name,
            //             ProductType: e.currentTarget.dataset.id
            //         })
            //     }
            // })
            var thisclas = this.data.RecType;
            for (var i = 0; i < thisclas.length; i++) {
                if (thisclas[i].id == this.data.ProductOne) {
                    if (thisclas[i].list.length > 0) {
                        if (thisclas[i].list[e.currentTarget.dataset.idx].list.length > 0) {
                            that.setData({
                                ProductThreeArr: thisclas[i].list[e.currentTarget.dataset.idx].list,
                                class_step: 3,
                                ProductThreeName: '全部',
                                ProductTypeName: e.currentTarget.dataset.name,
                                ProductType: e.currentTarget.dataset.id
                            })
                        }else{
                            that.setData({
                                ProductThreeArr: [],
                                ProductThreeName: '',
                                ProductTypeName: e.currentTarget.dataset.name,
                                ProductType: e.currentTarget.dataset.id
                            }) 
                            this.getSelectClass(2,e.currentTarget.dataset.id,e.currentTarget.dataset.name);   
                        }
                    }
                }
            }
        }else{
            this.setData({
                ProductTypeName: e.currentTarget.dataset.name,
                ProductType: e.currentTarget.dataset.id,
                ProductThreeArr:[],
                ProductThree:0,
                ProductThreeName:'',              
            })
            this.getSelectClass(1,this.data.ProductOne,this.data.ProductOneName);
        }        
    },
    selectC1:function (e) {
        var arr = this.data.RecType[e.currentTarget.dataset.idx].list;
        // console.log(arr.length);return false;
        if (e.currentTarget.dataset.id != this.data.Province) {
            this.setData({
                ProductTypeArr:[],
                ProductType:0,
                ProductTypeName:'',
                ProductThreeArr:[],
                ProductThree:0,
                ProductThreeName:'',              
            })
        }
        if (e.currentTarget.dataset.id != 0) {
            if (arr.length > 0) {
                this.setData({
                  ProductTypeName:'全部分类',
                  ProductOneName:e.currentTarget.dataset.name,
                  ProductOne:e.currentTarget.dataset.id,
                  ProductTypeArr:arr,
                  ProductThreeName:'',
                  class_step:2
                })                
            }else{
               this.setData({
                  ProductOneName:e.currentTarget.dataset.name,
                  ProductOne:e.currentTarget.dataset.id,
                  ProductTypeArr:[],
                  ProductThreeName:'',  
                  ProductTypeName:'',                
               }) 
               this.getSelectClass(1,e.currentTarget.dataset.id,e.currentTarget.dataset.name);
            }            
        }else{
            this.setData({
                ProductOne:0,
                ProductOneName:'全部分类',
                ProductTypeArr:[],
                ProductType:0,
                ProductTypeName:'',
                ProductThreeArr:[],
                ProductThree:0,
                ProductThreeName:'',
            })
            this.getSelectClass(0,0,'全部分类');
        }
    },
    addrBtn: function() {
        var tit_step = this.data.tit_step;
        var Province = this.data.Province;
        var ProvName = this.data.ProvName;
        var City = this.data.City;
        var CityName = this.data.CityName;
        var Area = this.data.Area;
        var AreaName = this.data.AreaName;
        if (tit_step == 3 && Area != 0) {
            this.getSelectAddr(3,Area,AreaName);
        }else if (tit_step == 3 && Area == 0) {
            this.getSelectAddr(2,City,CityName);
        }
        if (tit_step == 2 && City != 0) {
            this.setData({
                AreaArr: [],
                Area: 0,
                AreaName: '', 
            })
            this.getSelectAddr(2,City,CityName);
        }else if (tit_step == 2 && City == 0) {
            this.setData({
                AreaArr: [],
                Area: 0,
                AreaName: '', 
            })            
            this.getSelectAddr(1,Province,ProvName);
        }
    },
    // 选择省全省直接跳
    selectP: function(e) {
        var CityArr = this.data.ProvCity[e.currentTarget.dataset.idx].list;
        if (e.currentTarget.dataset.id != this.data.Province) {
            this.setData({
                AreaArr: [],
                Area: 0,
                AreaName: '',
                CityArr: [],
                City: 0,
                CityName: '',                
            })
        }
        if (e.currentTarget.dataset.id != 0) {
            this.setData({
                CityName: '全省',
                Province: e.currentTarget.dataset.id,
                ProvName: e.currentTarget.dataset.name,
                tit_step: 2,
                CityArr: CityArr
            })            
        }else{
            this.setData({
                Province: 0,
                ProvName: e.currentTarget.dataset.name,
                CityArr: [],
                City: 0,
                CityName: '',
                AreaArr: [],
                Area: 0,
                AreaName: ''
            })
            this.getSelectAddr(0,0,'全国');
        }
    },
    //选择市全市直接跳
    selectC: function(e) {
        var that = this;
        var api_input = {
            pid: e.currentTarget.dataset.id,
            lev: 3
        }
        if (e.currentTarget.dataset.id != 0) {
            app.getDaoPostN('api/FJDispose/FJ_Get_GetRegion', api_input, r => {
                if (r.code == 200) {
                    var ss = {id: 0,code: "0",name: "全城"}
                    r.data.unshift(ss);               
                    that.setData({
                        AreaArr: r.data,
                        City: e.currentTarget.dataset.id,
                        CityName: e.currentTarget.dataset.name,
                        tit_step: 3,
                        AreaName: '全城',
                    })
                }
            })
        }else{
            this.setData({
                City: e.currentTarget.dataset.id,
                CityName: e.currentTarget.dataset.name,
                AreaArr: [],
                Area: 0,
                AreaName: ''                
            })
            this.getSelectAddr(1,this.data.Province,this.data.ProvName);
        }
    },
    //选择区 全城直接跳
    selectA: function(e) {      
        this.setData({
            Area: e.currentTarget.dataset.id,
            AreaName: e.currentTarget.dataset.name,                
        })
        if (e.currentTarget.dataset.id == 0) {
            this.getSelectAddr(2,this.data.City,this.data.CityName);     
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.id) {
            this.setData({
                ProductOne:options.id,
                ProductOneName:options.name,
                ClassName:options.name,
                RTType: options.lev,
                RTID: options.id,
            })
        }
        if (options.key) {
            this.setData({
                Key:options.key
            })
        }
        this.getFJ_Get_ProvCity();
        this.getFJ_Get_RecType();
        // this.getStorageClass();
        var storageWeizhi = wx.getStorageSync('storageWeizhi');
        if (storageWeizhi.tit_step) {
        	this.getStorageAddr();
        	this.getFJ_Get_RecListKey();
        }else{
			var that = this;
			// wx.getLocation({
			// 	success: function(res) {
			// 		that.getweizhi(res.latitude, res.longitude);
			// 		// console.log(res)
			// 	},
			// 	fail: function(res) {
			// 		that.getFJ_Get_RecListKey()
			// 	}				
			// })	
        } 
    },

    getFJ_Get_ProvCity: function() {
        var that = this;
        var timestamp = Date.parse(new Date());
        var storageProvCity = wx.getStorageSync('storageProvCity');
        var storageProvCity_tamp = wx.getStorageSync('storageProvCity_tamp');
        if (storageProvCity && storageProvCity_tamp > timestamp) {
            this.setData({
                ProvCity: storageProvCity
            })
        } else {
            app.getDaoPostN('api/FJDispose/FJ_Get_ProvCity', '{}', function(r) {
                if (r.code == 200) {
                    wx.setStorageSync('storageProvCity', r.data);
                    wx.setStorageSync('storageProvCity_tamp', timestamp + 604800000);
                    that.setData({
                        ProvCity: r.data
                    })
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    wyczSearch: function(e) {
        this.setData({
            Key: app.trim(e.detail.value)
        })
    },
    searchBtn: function() {
        if (this.data.Key.length > 0) {
            this.setData({
                list: []
            })
            this.getFJ_Get_RecListKey();
        } else {
            app.tishi('请输入您要处置的产品名称')
        }

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        if (this.data.intCurrPage < this.data.intPageCount) {
            this.setData({
                intCurrPage: that.data.intCurrPage + 1
            })
            this.getFJ_Get_RecListKey();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    getFJ_Get_RecListKey: function() {
        var that = this;
        var api_input = this.api_input();
        // console.log(api_input);
        wx.showToast({
          title: "信息加载中..",
          icon: "loading",
          duration: 100000
        })
        app.getDaoPostN('api/FJDispose/FJ_Get_RecListKey', api_input, r => {
            if (r.code == 200) {
                for (var i = 0; i < r.data.data.ds.length; i++) {
                    if (r.data.data.ds[i].Photo) {
                        r.data.data.ds[i].Photo = r.data.data.ds[i].Photo.split('|')[0];
                    }
                }
                var ds = r.data.data.ds;
                var words = that.data.list.concat(ds);
                that.setData({
                    list: words,
                    intPageCount: r.data.intPageCount,
                    mores:true,
                })
                wx.hideToast();
            } else {
                wx.hideToast();
                app.tishi(r.message)
            }
            
        })
    },
    api_input: function() {
        var api_input = {
            UID: this.data.UID,
            Key: this.data.Key,
            IsSSK: this.data.IsSSK,
            ReginLev: this.data.ReginLev,
            Regin: this.data.Regin,
            RTType: this.data.RTType,
            RTID: this.data.RTID,
            Mode: this.data.Mode,
            FenShu: this.data.FenShu,
            intCurrPage: this.data.intCurrPage
        }
        return api_input;
    },
    toinfo: function(e) {
        wx.navigateTo({
            url: 'detailed?id=' + e.currentTarget.dataset.id
        })
    },
    fadeIn: function() {
        this.animation.translateY(0).step()
        this.setData({
            animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
        })
    },
    fadeDown: function() {
        this.animation.translateY(300).step()
        this.setData({
            animationData: this.animation.export(),
        })
    },
    zhuzhimaopao: function() {
        // body...
    },
    setpSet: function(e) {
        this.setData({
            tit_step: e.currentTarget.dataset.step
        })
    },
    setpSetCalss:function (e) {
        this.setData({
            class_step: e.currentTarget.dataset.step
        })
    },
    //地区显示
    selectS: function() {
        this.setData({
            selectS: true
        })
        var that = this;
        var animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'ease',
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        })
    },
    //分类显示
    selectClassS:function () {
        this.setData({
            selectClass: true
        })
        var that = this;
        var animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'ease',
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        })        
    },
    shaixuanS:function () {
        this.setData({
            shaixuan: true
        })
        var that = this;
        var animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'ease',
        })
        this.animation = animation
        setTimeout(function() {
            that.fadeIn(); //调用显示动画
        }) 
    },
    //地区隐藏
    selectH: function() {
        var that = this;
        // var animation = wx.createAnimation({
        //     duration: 200,
        //     timingFunction: 'ease',
        // })
        // this.animation = animation
        // that.fadeDown(); //调用隐藏动画   
        // setTimeout(function() {
            that.setData({
                selectS: false,
            })
        // }, 200)
    },
    //筛选隐藏
    shaixuanH:function () {
        var that = this;
        // var animation = wx.createAnimation({
        //     duration: 200,
        //     timingFunction: 'ease',
        // })
        // this.animation = animation
        // that.fadeDown(); //调用隐藏动画   
        // setTimeout(function() {
            that.setData({
                shaixuan: false,
            })
        // }, 200)
    },
    //分类隐藏
    selectClass: function() {
        var that = this;
        // var animation = wx.createAnimation({
        //     duration: 200,
        //     timingFunction: 'ease',
        // })
        // this.animation = animation
        // that.fadeDown(); //调用隐藏动画   
        // setTimeout(function() {
            that.setData({
                selectClass: false,
            })
        // }, 200)
    },    
    // 获取并保证选的地区信息
    getSelectAddr:function (ReginLev,Regin,CityNameTit) {
        this.setData({
            ReginLev: ReginLev,
            Regin: Regin,  
            CityNameTit:CityNameTit,
            list:[], 
        })
        this.selectH();
        this.getFJ_Get_RecListKey();
        var storageWeizhi = {
            ReginLev: ReginLev,
            Regin: Regin,  
            CityNameTit:CityNameTit,            
            Province: this.data.Province,
            ProvName: this.data.ProvName,
            CityArr: this.data.CityArr,
            City: this.data.City,
            CityName: this.data.CityName,
            AreaArr: this.data.AreaArr,
            Area: this.data.Area,
            AreaName: this.data.AreaName,           
            tit_step: this.data.tit_step           
        }
        wx.setStorageSync('storageWeizhi',storageWeizhi);
    },
    getSelectClass:function (RTType,RTID,ClassName) {
        this.setData({
            RTType: RTType,
            RTID: RTID,  
            ClassName:ClassName,
            list:[], 
        })
        this.selectClass();
        this.getFJ_Get_RecListKey();
        // var storageLeiBie = {
        //     RTType: RTType,
        //     RTID: RTID,  
        //     ClassName:ClassName,
        //     ProductOne:this.data.ProductOne,
        //     ProductOneName:this.data.ProductOneName,
        //     ProductTypeArr:this.data.ProductTypeArr,
        //     ProductType:this.data.ProductType,
        //     ProductTypeName:this.data.ProductTypeName,
        //     ProductThreeArr:this.data.ProductThreeArr,
        //     ProductThree:this.data.ProductThree,
        //     ProductThreeName:this.data.ProductThreeName,
        //     class_step:this.data.class_step
        // }
        // wx.setStorageSync('storageLeiBie',storageLeiBie); 
    },    
    getFJ_Get_RecType: function() {
        var that = this;
            app.getDaoPostN('api/FJDispose/FJ_Get_RecTypeList_New', '{}', function(r) {
                console.log(r)
                // if (r.code == 200) {
                //     wx.setStorageSync('storageRecType', r.data);
                //     wx.setStorageSync('storageRecType_tamp', timestamp + 604800000);
                    that.setData({
                        RecType: r.data
                    })
                // }
            })
        // }
    },

    getStorageAddr:function () {
        var storageWeizhi = wx.getStorageSync('storageWeizhi');
        if (storageWeizhi.tit_step) {
            this.setData({
                ReginLev: storageWeizhi.ReginLev,
                Regin: storageWeizhi.Regin,  
                CityNameTit:storageWeizhi.CityNameTit,            
                Province: storageWeizhi.Province,
                ProvName: storageWeizhi.ProvName,
                CityArr: storageWeizhi.CityArr,
                City: storageWeizhi.City,
                CityName: storageWeizhi.CityName,
                AreaArr: storageWeizhi.AreaArr,
                Area: storageWeizhi.Area,
                AreaName: storageWeizhi.AreaName,           
                tit_step: storageWeizhi.tit_step 
            })
        }
    },
    getStorageClass:function () {
        var storageLeiBie = wx.getStorageSync('storageLeiBie');
        if (storageLeiBie.class_step) {
            this.setData({
                RTType: storageLeiBie.RTType,
                RTID: storageLeiBie.RTID,  
                ClassName:storageLeiBie.ClassName,
                ProductOne:storageLeiBie.ProductOne,
                ProductOneName:storageLeiBie.ProductOneName,
                ProductTypeArr:storageLeiBie.ProductTypeArr,
                ProductType:storageLeiBie.ProductType,
                ProductTypeName:storageLeiBie.ProductTypeName,
                ProductThreeArr:storageLeiBie.ProductThreeArr,
                ProductThree:storageLeiBie.ProductThree,
                ProductThreeName:storageLeiBie.ProductThreeName,
                class_step:storageLeiBie.class_step
            })
        }
    }, 

    getLocalWeizhi:function () {
      var that = this;
      var storageWeizhi = wx.getStorageSync('storageWeizhi');
      if (storageWeizhi.class_step) {

      }else{
	      // wx.getLocation({
	      //   success:function (res) {
	      //     that.getweizhi(res.latitude,res.longitude);
	      //   }
	      // })      	
      }
    },

	getweizhi: function(latitude, longitude) {
		// var that = this;
		// wx.request({
		// 	url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${that.data.mapKey}`,
		// 	header: {
		// 		'Content-Type': 'application/json'
		// 	},
		// 	success: function(result) {
		// 		var r = result.data;
		// 		if (r.status == 0) {
		// 			var citystr = r.result.ad_info.adcode;
		// 			var cityName = r.result.ad_info.city;
		// 			var prcarr = that.data.ProvCity;
		// 			var list = [];
		// 			var City = 0;
		// 			var Province = 0;
		// 			var ProvName = '';
		// 			var CityArr = [];
		// 			for (var i = 0; i < prcarr.length; i++) {
		// 				if (prcarr[i].code == citystr.substring(0, 3) + '000') {
		// 					list = prcarr[i].list;
		// 					Province = prcarr[i].id;
		// 					ProvName= prcarr[i].name;
		// 				}
		// 			}
		// 			for (var j = 0; j < list.length; j++) {
		// 				if (list[j].code == citystr.substring(0, 5) + '0') {
		// 					City = list[j].id
		// 				}
		// 			}
		// 			var weizhi = {
		// 				CityNameTit: cityName,
		// 				CityName:cityName,
		// 				ProvName:ProvName,
		// 				Regin: City,
		// 				ReginLev: 2,
		// 				Province: Province,
		// 				City: City,
		// 				CityArr:list,
		// 				AreaArr:[],
		// 				Area:0,
		// 				AreaName:'',
		// 				tit_step:2
		// 			}
		// 			wx.setStorageSync('storageLocalWeizhi', weizhi);
		// 			that.setData({
		// 				CityNameTit: cityName,
		// 				CityName:cityName,
		// 				ProvName:ProvName,
		// 				Regin: City,
		// 				ReginLev: 2,
		// 				Province: Province,
		// 				City: City,
		// 				CityArr:list,
		// 				AreaArr:[],
		// 				Area:0,
		// 				AreaName:'',
		// 				tit_step:2
		// 			})
		// 			that.getFJ_Get_RecListKey()
		// 		}
		// 		// 
		// 	},
		// 	fail: function(res) {
    //     		that.getFJ_Get_RecListKey();
		// 	}
		// })
	},
	// getQu:function (id) {
 //        var that = this;
 //        var api_input = {
 //            pid: id,
 //            lev: 3
 //        }
 //        var AreaArr = [];
 //        app.getDaoPostN('api/FJDispose/FJ_Get_GetRegion', api_input, r => {
 //            if (r.code == 200) {
 //                var ss = {id: 0,code: "0",name: "全城"}
 //                r.data.unshift(ss); 
 //                AreaArr.push(r.data);
                
 //            }
 //        })		
	// 	console.log(AreaArr[0])
	// 	return AreaArr;
	// }
})